package com.hlingsoft.kkh.action.admin;

import com.hlingsoft.kkh.constant.EventCommentsConstant;
import com.hlingsoft.kkh.dao.EventDao;
import com.hlingsoft.kkh.entity.Event;
import com.hlingsoft.kkh.entity.vo.JsonVo;
import com.hlingsoft.kkh.entity.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin/event")
public class AdminEventAction extends AdminBaseAction {

	@Autowired
	private EventDao eventDao;

	static Map<String, Integer> perCapitaMap = new HashMap<String, Integer>() {
		{
			put("免费", 0);
			put("<100", 1);
			put("101~300", 2);
			put("301~500", 3);
			put("501~800", 4);
			put(">800", 5);
		}
	};

	@RequestMapping(value = "/merchantseventpage.htm", method = RequestMethod.GET)
	public String merchantseventpage(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) {

		PageVo<Event> pageVo = new PageVo<Event>(pageNum);
		pageVo.setRows(20);
		pageVo.setList(eventDao.getMerchantEvents(pageVo.getOffset(),
				pageVo.getRows()));
		pageVo.setCount(eventDao.getMerchantCount());
		modelMap.put("pageVo", pageVo);
		modelMap.put("basePath",
				"http://etimages.oss-cn-hangzhou.aliyuncs.com/upload/");
		return "system/event/allMerchantEvent";
	}

	@RequestMapping(value = "/usereventspage.htm", method = RequestMethod.GET)
	public String usereventspage(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) {

		PageVo<Event> pageVo = new PageVo<Event>(pageNum);
		pageVo.setRows(20);
		pageVo.setList(eventDao.getUserEvents(pageVo.getOffset(),
				pageVo.getRows()));
		pageVo.setCount(eventDao.getUserEventsCount());
		modelMap.put("basePath",
				"http://etimages.oss-cn-hangzhou.aliyuncs.com/upload/");
		modelMap.put("pageVo", pageVo);
		return "system/event/allUserEvent";
	}

	@RequestMapping(value = "/toAdd.htm", method = RequestMethod.GET)
	public String toAdd(HttpServletRequest request, ModelMap modelMap) {
		modelMap.put("kindId", System.currentTimeMillis());
		modelMap.put("JSESSIONID", request.getSession().getId());
		return "system/event/add";
	}

	@RequestMapping(value = "/delete.json")
	@ResponseBody
	public JsonVo<String> delete(@RequestParam(value = "eventId") long eventId,
			ModelMap modelMap) {
		JsonVo<String> json = new JsonVo<String>();
		eventDao.deleteEvent(eventId);
		json.setResult(true);
		return json;
	}

	@RequestMapping(value = "/detail.htm")
	public String detail(@RequestParam(value = "eventId") long eventId,
			ModelMap modelMap) {
		modelMap.put("eventVo", eventDao.getEventById(eventId));
		return "system/event/add";
	}

	SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm");

	@RequestMapping(value = "/add.json")
	@ResponseBody
	public JsonVo<String> add(
//			@RequestParam(value = "id") String id,
			@RequestParam(value = "pic") String pic,
			@RequestParam(value = "title") String title,
			@RequestParam(value = "startTime") String startTime,
			@RequestParam(value = "address") String address,
			@RequestParam(value = "longitude") String longitude,
			@RequestParam(value = "latitude") String latitude,
			@RequestParam(value = "ticketUrl") String ticketUrl,
			@RequestParam(value = "type") String type,
			@RequestParam(value = "perCapita") String perCapita,
			@RequestParam(value = "description") String description,
			ModelMap modelMap) {
		JsonVo<String> json = new JsonVo<String>();
		try {
			json.setResult(true);
//			if (StringUtils.isEmpty(id)) {

				Event event = new Event();
				event.setAddress(address);
				event.setCreateByUserId("1");
				event.setCreateTime(new Date());
				event.setType(type);
				event.setTitle(title);
				event.setPic(pic);
				event.setDescription(description);
				event.setLatitude(Float.valueOf(latitude));
				event.setLongitude(Float.valueOf(longitude));
				event.setStartTime(sf.parse(startTime));
				event.setStartTime2(sf.parse(startTime));
				event.setStartTime3(sf.parse(startTime));
				event.setStatus("display");
				event.setTop(0);
				event.setPriaseNum(0);
				event.setCommentNum(0);
				event.setVisibility(EventCommentsConstant.Visibility.square);
				event.setTicketUrl(ticketUrl);
				event.setAmount(0);
				event.setBuyCount(0);
				event.setEndTime(sf.parse(startTime));
				event.setPerCapitaInt(perCapitaMap.get(perCapita));
				event.setPerCapita(perCapita);
				event.setSource(EventCommentsConstant.CreateBySource.merchant);
				eventDao.addEvent(event);
//			}else{
//				eventDao.getEventById(Long.valueOf(eventId));
//			}
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			json.setMsg("输入参数有误");
			json.setResult(false);
			return json;
		}
	}

}
